package actions 
{
	import com.greensock.TweenLite;
	import core.App;
	import flash.display.Graphics;
	import flash.display.Shape;
	import view.scene.Scene;
	/**
	 * ...
	 * @author Johnny Cheng
	 */
	public class IrisSquareAction 
	{
		public var p:Number = 1;
		
		private var _radius:Number;
		
		private var _oldScene:Scene;
		private var _newScene:Scene;
		private var _oldMasker:Shape;
		private var _newMasker:Shape;
		
		public function IrisSquareAction(oldScene:Scene, newScene:Scene) 
		{
			if (oldScene == null) return;
			oldScene.mouseChildren = newScene.mouseChildren = false;
			newScene.visible = false;
			
			_oldScene = oldScene;
			_newScene = newScene;
			_radius = Math.sqrt(Math.pow(App.stage.stageWidth * .5, 2) + Math.pow(App.stage.stageHeight * .5, 2));
			
			_oldMasker = new Shape();
			_oldScene.addChild(_oldMasker);
			_oldScene.mask = _oldMasker;
			
			TweenLite.to(this, 0.4, { p:0, onUpdate:onOldUpdate, onComplete:onOldComplete } );
		}
		
		private function onOldUpdate():void 
		{
			drawMasker(_oldMasker.graphics);
		}
		
		private function onOldComplete():void 
		{
			_oldScene.removeChild(_oldMasker);
			_oldMasker = null;
			_oldScene.mask = null;
			_oldScene.removeSelf();
			
			_newScene.visible = true;
			_newMasker = new Shape();
			_newScene.addChild(_newMasker);
			_newScene.mask = _newMasker;
			TweenLite.to(this, 0.4, { p:1, onUpdate:onNewUpdate, onComplete:onNewComplete } );
		}
		
		private function onNewUpdate():void 
		{
			drawMasker(_newMasker.graphics);
		}
		
		private function onNewComplete():void 
		{
			_newScene.removeChild(_newMasker);
			_newMasker = null;
			_newScene.mask = null;
			_newScene.mouseChildren = true;
		}
		
		private function drawMasker(g:Graphics):void
		{
			g.clear();
			g.beginFill(0);
			var w:Number = App.stage.stageWidth * p;
			var h:Number = App.stage.stageHeight * p;
			g.drawRect((App.stage.stageWidth - w) * .5, (App.stage.stageHeight - h) * .5, w, h);
			g.endFill();
		}
	}

}